package com.resou.reader.reader.subsidy;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.resou.reader.R;
import com.resou.reader.api.entry.BookContentBean;
import com.resou.reader.api.entry.SubSidyDetailBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.reader.adpter.SubSidyAdapter;
import com.resou.reader.reader.adpter.SubSidyExplanAdapter;
import com.resou.reader.utils.AutoHeightLayoutManager;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.PreferenceUtil;
import com.resou.reader.utils.SHeightLayoutManager;
import com.resou.reader.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 补贴模式页面
 */
public class SubSidyActivity extends ResouBaseActivity<SubSidyPresenter> implements ISubSidyContract.ISubSidyView {

    private static int TAGRESULT = 0x001;

    /**
     * 启动方法
     *
     * @param context
     */
    public static void start(Activity context) {
        Intent starter = new Intent(context, SubSidyActivity.class);
        context.startActivityForResult(starter, TAGRESULT);

    }

    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mTitleBar)
    RelativeLayout mTitleBar;
    @BindView(R.id.sbSubSidy)
    SwitchButton sbSubSidy;
    @BindView(R.id.tvDayMoney)
    TextView tvDayMoney;
    @BindView(R.id.tvAllMoney)
    TextView tvAllMoney;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRecyclerViewExplan)
    RecyclerView mRecyclerViewExplan;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.mScrollView)
    ScrollView mScrollView;
    SubSidyAdapter subSidyAdapter;
    SubSidyExplanAdapter subSidyExplanAdapter;
    @BindView(R.id.tvSubSidyInfo)
    TextView tvSubSidyInfo;
    AlertDialog alertDialog = null;
    TextView tvDialogTitle;
    TextView tvOk;
    LinearLayout llExit;
    TextView tvContent;
    TextView tvCancelExit;
    TextView tvOkExit;
    View view = null;

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new SubSidyPresenter(this);
    }

    @Override
    protected void initView() {
        subSidyExplanAdapter = new SubSidyExplanAdapter(R.layout.adapter_subsidy_explan, null);
        mRecyclerViewExplan.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerViewExplan.setAdapter(subSidyExplanAdapter);
        subSidyAdapter = new SubSidyAdapter(R.layout.adapter_subsidy, null);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(subSidyAdapter);
        initDialog();
        sbSubSidy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    sbSubSidy.setBackColor(ColorStateList.valueOf(getResources().getColor(R.color.color4cda64)));
                } else {
                    sbSubSidy.setBackColor(ColorStateList.valueOf(getResources().getColor(R.color.color_A6A9AE)));
                }
            }
        });
        sbSubSidy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        sbSubSidy.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (!NetworkUtils.isNetworkConnected(SubSidyActivity.this)) {
                        ToastUtil.makeShortToast("网络异常");
                        return true;
                    }
                    alertDialog.show();
                }
                return true;
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sub_sidy;
    }

    @Override
    public void retryRefreshData() {
        if (mPresenter != null) {
            mPresenter.quearSubSidyList();
        }
    }

    /**
     * 设置成功
     */
    @Override
    public void setSubSidySucess() {
        setSubState(PreferenceUtil.getInstance(this).getSubSidyStateValue());
        EventBus.getDefault().post(new BookContentBean());
    }

    /**
     * 设置失败
     */
    @Override
    public void setSubSidyFail() {
        setSubState(PreferenceUtil.getInstance(this).getSubSidyStateValue());
    }

    @Override
    public void showData(SubSidyDetailBean subSidyDetailBean) {
        mScrollView.setBackgroundColor(getResources().getColor(R.color.divider));
        subSidyExplanAdapter.setNewData(subSidyDetailBean.getSubsidyExplain());
        setExplan(subSidyDetailBean);
        if (subSidyDetailBean.getLatestSubsidy() == null || subSidyDetailBean.getLatestSubsidy().size() == 0) {
            llContent.setVisibility(View.GONE);
            return;
        }
        subSidyAdapter.setNewData(subSidyDetailBean.getLatestSubsidy());
    }

    @Override
    public Context getContext() {
        return this;
    }


    @Override
    public void initData() {
        if (mPresenter != null) {
            mPresenter.quearSubSidyList();
        }
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        dissLoading();
        ToastUtil.makeShortToast("网络异常");
        mScrollView.setBackgroundColor(getResources().getColor(R.color.white));
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {

    }

    @OnClick(R.id.ivBack)
    public void onClick() {
        finish();
    }


    /**
     * 显示阅读进度不同的弹框
     */
    private void initDialog() {
        if (view == null) {
            view = LayoutInflater.from(this).inflate(R.layout.dialog_subsidy, null);
            tvDialogTitle = view.findViewById(R.id.tvDialogTitle);
            tvOk = view.findViewById(R.id.tvOk);
            llExit = view.findViewById(R.id.llExit);
            tvCancelExit = view.findViewById(R.id.tvCancelExit);
            tvContent = view.findViewById(R.id.tvContent);
            tvOkExit = view.findViewById(R.id.tvOkExit);
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);
        builder.setView(view);
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.setSubSidy(0);
                if (alertDialog.isShowing()) {
                    alertDialog.dismiss();
                }
            }
        });
        tvCancelExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sbSubSidy.setChecked(true);
                if (alertDialog.isShowing()) {
                    alertDialog.dismiss();
                }
            }
        });
        tvOkExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.setSubSidy(2);
                if (alertDialog.isShowing()) {
                    alertDialog.dismiss();
                }
            }
        });
        alertDialog = builder.create();
    }


    /**
     * 填充金额与补充说明数据
     *
     * @param subSidyDetailBean
     */
    private void setExplan(SubSidyDetailBean subSidyDetailBean) {
        tvDayMoney.setText(subSidyDetailBean.getSubsidyCollect().getDailyPrice() + "");
        tvAllMoney.setText(subSidyDetailBean.getSubsidyCollect().getTotalPrice() + "");
        setSubState(subSidyDetailBean.getReadType());
    }

    /**
     * 设置补贴模式状态
     *
     * @param subsidystate
     */
    private void setSubState(int subsidystate) {
        if (subsidystate == 0) {
            tvSubSidyInfo.setText("已开启补贴模式");
            tvDialogTitle.setText("确定退出补贴模式？");
//            sbSubSidy.setCheckedImmediately(true);
            sbSubSidy.setChecked(true);
            tvOk.setVisibility(View.GONE);
            llExit.setVisibility(View.VISIBLE);
            tvContent.setText("退出补贴模式后,VIP章节恢复收费哦");
        } else {
            tvSubSidyInfo.setText("未开启补贴模式");
            tvDialogTitle.setText("您已开启补贴模式");
//            sbSubSidy.setCheckedImmediately(false);
            sbSubSidy.setChecked(false);
            tvOk.setVisibility(View.VISIBLE);
            llExit.setVisibility(View.GONE);
            tvContent.setText("如想关闭补贴模式，请在阅读页点击屏幕中央，选择设置 > 点击[退出补贴模式]按钮，进行关闭");
        }
        Log.i("ddd", "setSubState: " + sbSubSidy.isChecked());
    }
}
